package com.itqf.Controller;

import com.github.pagehelper.PageInfo;
import com.itqf.Enum.CodeEnum;
import com.itqf.Service.Topic.TopicService;
import com.itqf.Util.DTO.ResultDTO;
import com.itqf.Util.myPageInfo;
import com.itqf.entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/huileme/a/m/TopicController")
public class TopicController {

    @Resource
    private TopicService topicService;
    @RequestMapping(value = "/homePage",method = RequestMethod.GET)
    @ResponseBody
    /**
        * @Description:
        * @Param: [subjectId, gradeid]
        * @return: com.itqf.entity.Topicjson
        * @Author: 赵渊
        * @Date: 20:23
        */ //首页显示的所有数据
    public ResultDTO<Topicjson> getTopicjson(@RequestParam(defaultValue = "-1") int subjectId, @RequestParam(defaultValue = "-1") int gradeid)
    {
        ResultDTO resultDTO=new ResultDTO();
        //1.根据年级查询每个年级所有的科目的
        List<Subject> findsubjects = topicService.findsubjects(gradeid);
        //2.通过科目显示精品推荐的商品
        List<Topic> topicList=topicService.findtopic(subjectId);
        Topicjson topicjson=new Topicjson();
        topicjson.setSubjectsList(findsubjects);
        topicjson.setTopicList(topicList);
        //3.查找所有年级用于下拉框选择年级
        List<Topicstage> findtopicstagelist = topicService.findtopicstagelist();
        topicjson.setTopicstageList(findtopicstagelist);
        //4.查找轮播图的数据
        List<Rotationchart> rotationchartList = topicService.getrotationchart();
        topicjson.setRotationchartList(rotationchartList);
        if (findsubjects!=null&&topicList!=null&&findtopicstagelist!=null&&rotationchartList!=null)
      {
           resultDTO.setData(topicjson);
           resultDTO.setCode(CodeEnum.selectsuccess.getCode());
           resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
      }else
      {
          resultDTO.setCode(CodeEnum.selecterror.getCode());
          resultDTO.setMessage(CodeEnum.selecterror.getMsg());
      }

        return resultDTO;
    }

    @RequestMapping(value = "/search",method = RequestMethod.POST)
    @ResponseBody
    /**
        * @Description:
        * @Param: [searchname, pageindex]
        * @return: com.github.pagehelper.PageInfo
        * @Author: 赵渊
        * @Date: 20:23
        *///首页搜索框的内容,其他页搜索框也可以调用该接口
    public ResultDTO<PageInfo> gettopicslist(String searchname,@RequestParam(defaultValue = "1") int pageindex)
    {
       PageInfo<Topic>  pageInfo=topicService.findbysearchname(pageindex, myPageInfo.PAGESIZE,searchname);

       ResultDTO resultDTO=new ResultDTO();
        if (!pageInfo.getList().isEmpty())
        {
            resultDTO.setData(pageInfo);
            resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
            resultDTO.setCode(CodeEnum.selectsuccess.getCode());
        }else
        {

            resultDTO.setMessage(CodeEnum.selecterror.getMsg());
            resultDTO.setCode(CodeEnum.selecterror.getCode());
        }
       return resultDTO;
    }

}
